<?php
declare (strict_types = 1);
namespace app\admin\controller;
use app\common\controller\Backend;
use think\facade\View;


class Ad extends Backend
{
    private $_ad_type = array('image'=>'图片', 'code'=>'代码', 'text'=>'文字');
    public $list_relation = true;
    public function initialize() {
        parent::initialize();
        $this->_mod  = new \app\admin\model\Ad();
        $this->_adboard_mod = new \app\admin\model\Adboard();
        View::assign('img_dir', attach('','advert'));
    }


    public function _before_index(){
        $where = array();
        $keyword =  request()->param('keyword');
        if($keyword) $where[]=array('name','like','%'.$keyword.'%');
        View::assign('search', array(
            'keyword'=>$keyword,
        ));
        $this->_list($this->_mod, $where, 'id', 'desc');
        $res = $this->_adboard_mod->field('id,name')->select();
        $board_list = array();
        foreach ($res as $val) {
            $board_list[$val['id']] = $val['name'];
        }
        View::assign('board_list', $board_list);
        View::assign('ad_type_arr', $this->_ad_type);
    }



    public function _before_add() {
        $result = $this->_adboard_mod->where(array('status'=>1))->select()->toArray();
        $adboard_types = $this->_adboard_mod->get_tpl_list();
        $adboards = array();
        foreach ($result as $val) {
            $val['allow_type'] = implode('|', $adboard_types[$val['tpl']]['allow_type']);
            $adboards[] = $val;
        }
        View::assign('id', 0);
        View::assign('adboards', $adboards);
        View::assign('ad_type_arr', $this->_ad_type);
    }

    public function _handle_insert($data) {

        switch ($data['type']) {
            case 'text':
                $data['content'] = trim(request()->param('content'));
                break;
            case 'image':
                $data['content'] =  trim(request()->param('img'));
                break;
            case 'code':
                $data['content'] = trim(request()->param('code'));
                break;
            default :
               return fail(0,'类型错误');
                break;
        }
        return $data;
    }

    public function _before_edit() {
        $id = request()->param('id');
        $board_id = $this->_mod->where(array('id'=>$id))->value('board_id');
        $board_info = $this->_adboard_mod->field('name,width,height')->where(array('id'=>$board_id))->find();
        View::assign('id', $id);
        View::assign('board_info', $board_info);
        View::assign('ad_type_arr', $this->_ad_type);
    }

    public function _handle_update($data) {
        //判断开始时间和结束时间是否合法
        return $this->_handle_insert($data);
    }

    //上传图片
    public function ajax_upload_img(){
        $file = request()->file('file');  //这里‘file’是你提交时的name
        $info = [
            'ext'=>$file->extension(),
            'filesize' => $file->getSize(),    // 文件名：读书顶个鸟用.jpg
            'name' => $file->getFilename(),    // 文件MINE：image/jpeg
            'mime' => $file->getMime(),
            'oldname'=>$file->getOriginalName()
        ];
        try{
            $saveName = \think\facade\Filesystem::disk('public')->putFile( 'ad', $file,
                function () use ($file){
                    switch ('date') {
                        case 'date':
                            $fileName = date('YmdHis').rand(1,99);
                            break;
                        // 此项即为：保留原始文件名+日期方式
                        case 'original':
                            $fileName = str_replace('.'.$file->getOriginalExtension(),'',$file->getOriginalName().date('YmdHis'));
                            break;
                        default:
                            $fileName = uniqid();
                            break;
                    }
                    return $fileName;
                });//保存文件名
            $newname= str_replace('ad/','',$saveName);
            $arr = ['status' => 1, 'msg' => '成功', 'info' => $newname,'img_url'=>upload_url($newname,  'ad')];
            return json($arr);   				//返回标准json格式
        }catch (\Exception $e) {
            return $this->exceptionHandle($e,'上传失败!' . $e->getMessage(),'json','');
        }
    }

}


